//
//  ChangePassWordViewController.m
//  gpos_app
//
//  Created by 崔航飞 on 2018/8/18.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ChangePassWordViewController.h"
#import "Masonry.h"
#import "WKProgressHUD.h"
#import "GPosAPI.h"
#import "SVProgressHUD.h"
@interface ChangePassWordViewController ()<UIGestureRecognizerDelegate,UITextFieldDelegate>
@property (nonatomic,strong) UIView *backgroundViews;
@property (nonatomic,strong) UITextField *textField1;
@property (nonatomic,strong) UITextField *textField2;
@property (nonatomic,strong) UITextField *textField3;
@property (nonatomic,strong) UIImageView *BottomView;
@property(strong,nonatomic)WKProgressHUD *hud;

@end

@implementation ChangePassWordViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=NO;
    //去掉返回的文字
    self.navigationController.navigationBar.topItem.title = @"";
    self.navigationItem.title=@"修改密码";
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
        
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor whiteColor];
    [self initUI];
}
-(void)initUI{
    
    self.backgroundViews=[[UIView alloc] init];
    self.backgroundViews.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.backgroundViews];
    [self.backgroundViews mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view.mas_top).offset(kNavbarHeight);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.equalTo(@(KmainScreenHeiht - kNavbarHeight - kBottomSafeHeight));
        
    }];
    
    UIImageView *imgOne=[[UIImageView alloc] init];
    imgOne.image=[UIImage imageNamed:@"icon_mima_l"];
    [self.backgroundViews addSubview:imgOne];
    [imgOne mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.backgroundViews.mas_left).offset(32);
        make.top.equalTo(self.backgroundViews.mas_top).offset(30);
        
    }];
    

    UIView *line1=[[UIView alloc] init];
    line1.backgroundColor=[UIColor blueColor];
    [self.backgroundViews addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.backgroundViews.mas_left).offset(32);
        make.right.equalTo(self.backgroundViews.mas_right).offset(-32);
        make.top.equalTo(imgOne.mas_bottom).offset(6);
        make.height.equalTo(@(0.8));
        
    }];
    
    self.textField1=[[UITextField alloc] init];
    self.textField1.placeholder=@"请输入旧密码";
    self.textField1.delegate=self;
    [self.backgroundViews addSubview:self.textField1];
    [self.textField1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(imgOne.mas_right).offset(20);
        make.top.equalTo(self.backgroundViews.mas_top).offset(30);
        make.bottom.equalTo(line1.mas_top).offset(-6);
        make.width.equalTo(@(200));
    }];
    
    UIImageView *imgTwo=[[UIImageView alloc] init];
    imgTwo.image=[UIImage imageNamed:@"icon_mima_l"];
    [self.backgroundViews addSubview:imgTwo];
    [imgTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.backgroundViews.mas_left).offset(32);
        make.top.equalTo(line1).offset(30);
        
    }];
    
    
    UIView *line2=[[UIView alloc] init];
    line2.backgroundColor=[UIColor blueColor];
    [self.backgroundViews addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.backgroundViews.mas_left).offset(32);
        make.right.equalTo(self.backgroundViews.mas_right).offset(-32);
        make.top.equalTo(imgTwo.mas_bottom).offset(6);
        make.height.equalTo(@(0.8));
        
    }];
    
    self.textField2=[[UITextField alloc] init];
    self.textField2.placeholder=@"请输入新密码(8-15个字符)";
    self.textField2.delegate=self;
    [self.backgroundViews addSubview:self.textField2];
    [self.textField2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(imgTwo.mas_right).offset(20);
        make.top.equalTo(line1.mas_top).offset(30);
        make.bottom.equalTo(line2.mas_top).offset(-6);
        make.width.equalTo(@(240));
    }];
    
    
    
    UIImageView *imgThree=[[UIImageView alloc] init];
    imgThree.image=[UIImage imageNamed:@"icon_mima_l"];
    [self.backgroundViews addSubview:imgThree];
    [imgThree mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.backgroundViews.mas_left).offset(32);
        make.top.equalTo(line2).offset(30);
        
    }];
    
    
    UIView *line3=[[UIView alloc] init];
    line3.backgroundColor=[UIColor blueColor];
    [self.backgroundViews addSubview:line3];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.backgroundViews.mas_left).offset(32);
        make.right.equalTo(self.backgroundViews.mas_right).offset(-32);
        make.top.equalTo(imgThree.mas_bottom).offset(6);
        make.height.equalTo(@(0.8));
        
    }];
    
    self.textField3=[[UITextField alloc] init];
    self.textField3.placeholder=@"请再次输入新密码";
    self.textField3.delegate=self;
    [self.backgroundViews addSubview:self.textField3];
    [self.textField3 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(imgThree.mas_right).offset(20);
        make.top.equalTo(line2.mas_top).offset(30);
        make.bottom.equalTo(line3.mas_top).offset(-6);
        make.width.equalTo(@(240));
    }];
    
    
    //确认按钮
    self.BottomView =[[UIImageView alloc] init];
    self.BottomView.image=[UIImage imageNamed:@"icon_anniu_f"];
    self.BottomView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(QRBtn)];
    tap.delegate=self;
    [self.BottomView addGestureRecognizer:tap];
    [self.backgroundViews addSubview:self.BottomView];
    [self.BottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(line3.mas_bottom).offset(45);
        make.left.equalTo(self.backgroundViews.mas_left).offset(32);
        make.right.equalTo(self.backgroundViews.mas_right).offset(-32);
    
    }];
    
    UILabel *backLab=[[UILabel alloc] init];
    backLab.text = @"确认";
    backLab.userInteractionEnabled =YES;
    backLab.textColor = [UIColor whiteColor];
    backLab.textAlignment = NSTextAlignmentCenter;
    [self.BottomView addSubview:backLab];
    [backLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.BottomView.mas_top).offset(25);
        make.left.equalTo(self.BottomView.mas_left);
        make.right.equalTo(self.BottomView.mas_right);
        make.height.equalTo(@(30));
    }];
    
}
-(void)QRBtn{
    
    if (self.textField1.text.length==0)
    {
        self.hud=[WKProgressHUD popMessage:@"请输入旧密码" inView:self.view duration:1 animated:YES];
    }else if (self.textField2.text.length==0){
        
         self.hud=[WKProgressHUD popMessage:@"请输入新密码" inView:self.view duration:1 animated:YES];
        
    }else if (self.textField3.text.length==0){
        
        self.hud=[WKProgressHUD popMessage:@"请输入确认密码" inView:self.view duration:1 animated:YES];
        
    }else if (self.textField2.text.length<8){
        
        self.hud=[WKProgressHUD popMessage:@"新密码请输入8-15个字符" inView:self.view duration:1 animated:YES];
        
    }else if (self.textField3.text.length<8){
        
        self.hud=[WKProgressHUD popMessage:@"确认密码请输入8-15个字符" inView:self.view duration:1 animated:YES];
        
    }
    else{//网络请求
        
        __weak typeof(self) weakSelf = self;
        /*
         参数1：phone
         参数2：oldPassword
         参数3：newPassword
         参数4：repeatPassword
         参数5：type
         */
        NSString *phone=[[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:phone,@"phone",self.textField1.text,@"oldPassword",self.textField2.text,@"newPassword",self.textField3.text,@"repeatPassword",@(1),@"type", nil];
        [GPosAPI requestChangeloginPasswordWithParams:dic andCallback:^(id obj) {
            if (obj==nil)
            {
                
                [SVProgressHUD showErrorWithStatus:@"请求失败,请检查您的网络状态"];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
            }else{
                
                if ([[obj objectForKey:@"code"] integerValue] ==200)
                {
                    
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeYes" object:nil];
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                    
                    
                }else{
                    
                    [SVProgressHUD showErrorWithStatus:[obj objectForKey:@"message"]];
                    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                    
                }
            }
        }];
        
    }
    
}
#pragma textFieldDelagate(限制输入的电话长度)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.textField1 ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.textField1.text.length >= 15) {
            self.textField1.text = [textField.text substringToIndex:15];
            
            return NO;
        }
    }
    if (textField == self.textField3 ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.textField3.text.length >= 15) {
            self.textField3.text = [textField.text substringToIndex:15];
            
            return NO;
        }
    }
    if (textField == self.textField2 ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.textField2.text.length >= 15) {
            self.textField2.text = [textField.text substringToIndex:15];
            
            return NO;
        }
        //设置密文切换时保持之前的输入内容
        NSString *allstr=[textField.text stringByReplacingCharactersInRange:range withString:string];
        if (self.textField2.isSecureTextEntry ==YES)
        {
            self.textField2.text=allstr;
            return NO;
        }
    }
    

    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
